package asynqwork

import (
	"context"
	"encoding/json"
	"github.com/gogf/gf/v2/frame/g"
	"home-butler/internal/model"
	"home-butler/internal/service"
)

var ScheduledMessageUp = new(Scheduled)

func ScheduledMessageUpRun() {
	ScheduledMessageUp = RegisterProcess(MessageUp)
}

var MessageUp = &qMessageUp{}

type qMessageUp struct{}

// GetTopic 主题
func (q *qMessageUp) GetTopic() string {
	return "message_up_channel"
}

// Handle 处理消息
func (q *qMessageUp) Handle(ctx context.Context, p Payload) (err error) {
	g.Log().Infof(ctx, "alarm_Handle: Payload(%s)", p.Payload)

	if p.Payload == nil || q.GetTopic() != p.Group {
		return nil
	}

	var data model.MessageReq
	if err = json.Unmarshal(p.Payload, &data); err != nil {
		return err
	}

	if err = service.Message().MessageParsing(context.Background(), &data); err != nil {
		g.Log("入库错误")
	}
	return
}
